package hn.sigit.view;

import hn.sigit.dao.hnd.ladmshadow.ParcelDAO;
import hn.sigit.dao.hnd.ladmshadow.PropertyDAO;
import hn.sigit.logic.general.ResourceBundleHelper;
import hn.sigit.model.hnd.ladmshadow.Parcel;
import hn.sigit.model.hnd.ladmshadow.Property;
import hn.sigit.model.hnd.ladmshadow.RRR;
import hn.sigit.model.hnd.ladmshadow.Responsibility;
import hn.sigit.model.hnd.ladmshadow.Restriction;
import hn.sigit.model.hnd.ladmshadow.Right;
import hn.sigit.model.ladm.external.ExtParty;
import hn.sigit.util.ShareValue;
import hn.sigit.logic.viewer.toolbox.ParcelResponsibility;
import hn.sigit.logic.viewer.toolbox.ParcelRestriction;
import hn.sigit.logic.viewer.toolbox.ParcelRight;
import hn.sigit.logic.viewer.toolbox.RRRHelperLogic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("viewTransactionHelper")
@Scope(value = ScopeType.CONVERSATION)
@AutoCreate
public class ViewTransactionHelper implements Serializable {
	private static final long serialVersionUID = 1L;

	private boolean renderTransactionData = false;
	
	private long baUnitId = 0;
	private long suID = 0;
	
	private Property property;
	private Set<RRR> rrrSet;
	private Parcel parcel;
	
	private List<ParcelRight> parcelRightsList;
	private List<ParcelRestriction> parcelRestrictionsList;
	private List<ParcelResponsibility> parcelResponsibilitiesList;
	
	@In
	private ResourceBundleHelper resBundle;


	public boolean isRenderTransactionData() {
		return renderTransactionData;
	}
	public void setRenderTransactionData(boolean renderTransactionData) {
		this.renderTransactionData = renderTransactionData;
	}

	public long getBaUnitId() {
		return this.baUnitId;
	}
	public void setBaUnitId(long baUnitId) {
		if (baUnitId != this.baUnitId) {
			invalidateRRRsList();
			this.baUnitId = baUnitId;
		}
	}
	
	public long getSuID() {
		return suID;
	}
	public void setSuID(long suID) {
		if (suID != this.suID) {
			this.parcel = null; 
			this.suID = suID;
		}
	}
	
	private Property getProperty() {
		if (property == null) {
			property = PropertyDAO.loadPropertyById(baUnitId);
		}
		return property;
	}
	private Set<RRR> getRrrSet() {
		if (rrrSet == null) {
			if (getProperty() != null)
				rrrSet = getProperty().getRrr();
		}
		return rrrSet;
	}
	
	public Parcel getParcel(String suID) {
		setSuID(Long.valueOf(suID));
		
		if (parcel == null) {
			parcel = ParcelDAO.loadParcelById(this.suID);
		}
		return parcel;
	}
	
	public Parcel getParcel() {
		return getParcel(String.valueOf(suID));
	}
	
	public List<ParcelRight> getParcelRightsList(String baUnitId) {
		setBaUnitId(Long.valueOf(baUnitId));
		
		if (parcelRightsList == null) {
			parcelRightsList = new ArrayList<ParcelRight>();
			
			if (getRrrSet() != null) {
				ParcelRight pr;
				for (RRR rrr : getRrrSet()) {
					pr = null;
					if (rrr instanceof Right) {
						Right right = (Right)rrr;
						ExtParty extParty = right.getParty().getExtParty();
						pr = new ParcelRight(null, extParty, right.getType(),
								new ShareValue(right.getShare()), right);
					}
					
					if (pr != null)
						parcelRightsList.add(pr);
				}
			}
		}
		return parcelRightsList;
	}
	public ShareValue getRightsSumShares() {
		return RRRHelperLogic.getSumShares(parcelRightsList);
	}

	public List<ParcelRestriction> getParcelRestrictionsList(String baUnitId) {
		setBaUnitId(Long.valueOf(baUnitId));
		
		if (parcelRestrictionsList == null) {
			parcelRestrictionsList = new ArrayList<ParcelRestriction>();
			
			if (getRrrSet() != null) {
				ParcelRestriction pr;
				for (RRR rrr : getRrrSet()) {
					pr = null;
					if (rrr instanceof Restriction) {
						Restriction restriction = (Restriction)rrr;
						ExtParty extParty = restriction.getParty().getExtParty();
						pr = new ParcelRestriction(null, extParty, restriction.getType(),
								new ShareValue(restriction.getShare()), restriction);
					}
					
					if (pr != null)
						parcelRestrictionsList.add(pr);
				}
			}
		}
		return parcelRestrictionsList;
	}
	public ShareValue getRestrictionsSumShares() {
		return RRRHelperLogic.getSumShares(parcelRestrictionsList);
	}

	public List<ParcelResponsibility> getParcelResponsibilitiesList(String baUnitId) {
		setBaUnitId(Long.valueOf(baUnitId));

		if (parcelResponsibilitiesList == null) {
			parcelResponsibilitiesList = new ArrayList<ParcelResponsibility>();

			if (getRrrSet() != null) {
				ParcelResponsibility pr;
				for (RRR rrr : getRrrSet()) {
					pr = null;
					if (rrr instanceof Responsibility) {
						Responsibility responsibility = (Responsibility)rrr;
						ExtParty extParty = responsibility.getParty().getExtParty();
						pr = new ParcelResponsibility(null, extParty, responsibility.getType(),
								new ShareValue(responsibility.getShare()), responsibility);
					}
					
					if (pr != null)
						parcelResponsibilitiesList.add(pr);
				}
			}
		}
		return parcelResponsibilitiesList;
	}
	public ShareValue getResponsibilitiesSumShares() {
		return RRRHelperLogic.getSumShares(parcelResponsibilitiesList);
	}

	private void invalidateRRRsList() {
		property = null;
		rrrSet = null;
		parcelRightsList = null;
		parcelRestrictionsList = null;
		parcelResponsibilitiesList = null;
	}
	
	public String formatEmptyText(Object value) {
		if (value == null || value.equals(""))
			return resBundle.loadMessage("global.empty");
		
		return value.toString();
	}
	
	public String getPrimaryLandUseText() {
		if (parcel == null || parcel.getPrimaryLandUse() == null)
			return resBundle.loadMessage("global.empty");

		return parcel.getPrimaryLandUse().getFullName();
	}
	public String getSecondaryLandUseText() {
		if (parcel == null || parcel.getSecondaryLandUse() == null)
			return resBundle.loadMessage("global.empty");

		return parcel.getSecondaryLandUse().getFullName();
	}

}
